<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" media="all">
    <script src="../layui/layui.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="demo"></table>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script>
    layui.use(['table', 'jquery'], function () {
        var table = layui.table
            , $ = layui.jquery;

        table.render({
            elem: '#test'
            , url: '/hospital/manager/user/finddoctor.do'
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                {field: 'id', width: 80, title: 'ID', sort: true}
                , {field: 'name', width: 80, title: '用户名',edit: 'text'}
                , {field: 'password', width: 80, title: '密码', edit: 'text'}
                , {field: 'sex', width: 80, title: '性别'}
                , {field: 'phone', title: '手机', width: 160, edit: 'text',minWidth: 100} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                , {field: 'idNumber', title: '身份证',width: 250}
                , {field: 'jobName', title: '职称',width: 120, edit: 'text'}
                , {field: 'departName', title: '部门', width: 120}
                ,{fixed: 'right', width:178, align:'center', toolbar: '#barDemo'}
            ]]
            , page: true
        });

        //监听工具条
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url:'/hospital/manager/user/delete.do',
                        type: 'post',
                        data:{"id":data.id},
                        dataType: 'json',
                        success:function (data) {
                            if (data.code === 1){
                                obj.del();
                                layer.close(index);
                                layer.msg(data.info);
                            } else {
                                layer.msg("删除失败")
                            }
                        }
                    })

                });
            }
        });

        //监听单元格编辑
        table.on('edit(demo)', function(obj){
            var value = obj.value //得到修改后的值
                ,id = obj.data.id //得到所在行所有键值
                ,field = obj.field; //得到字段
            $.ajax({
                url:'/hospital/manager/user/updateuser.do',
                type:'post',
                data:obj.data,
                dataType:'json',
                success:function (data){
                    if (data.code === 1) {
                        layer.msg('[ID: '+ id +'] ' + field + ' 字段更改为：'+ value);
                    } else {
                        layer.msg("更改失败");
                    }
                }
            })
        });

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</body>
</html>